const bcrypt = require('bcrypt');

const {User} = require('../../model/user')

module.exports = async (req, res) => {
    const {email, password} = req.body;
    if(!email.trim().length || !password.trim().length){
         res.status(400).render("admin/error", {msg: "邮件地址或者密码错误"});
         return ;
    }

    let user = await User.findOne({email});
    let isEqual = await bcrypt.compare(password, user.password);

    if(!user || !isEqual){
        res.status(400).render("admin/error", {msg: "邮件地址或者密码错误"});
        return ;
    }
    // 将用户名存储在请求对象中 
    req.session.username = user.username;
    // 在这里拿到的就是app.js里面的app对象
    req.app.locals.userInfo = user;
    // res.send("登录成功！");
    res.redirect("/admin/article");
}